package com.example.jacky.uilibrary.utils;

import android.content.res.Resources;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.view.View;
import android.widget.Toast;

/**
 * Created by jacky on 2016/9/30.
 * sounbus app-developer
 */

public class Utils {

    public static Resources getResource() {
        return LibData.getContext().getResources();
    }

    /**
     * px转换dp
     *
     * @param px
     * @return
     */
    public static int px2dip(int px) {
        final float scale = getResource().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }

    /**
     * dp转dx
     *
     * @param dip
     * @return
     */
    public static int dip2px(int dip) {
        final float scale = getResource().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f);
    }

    /**
     * 获取状态栏高度
     *
     * @param v
     * @return
     */
    public static int getStatusBarHeight(View v) {
        if (v == null) {
            return 0;
        }
        Rect frame = new Rect();
        v.getWindowVisibleDisplayFrame(frame);
        return frame.top;
    }

    public static Toast mToast;

    public static void showToast(String msg) {
        if (mToast == null) {
            mToast = Toast.makeText(LibData.getContext(), msg, Toast.LENGTH_SHORT);
        }
        mToast.setText(msg);
        mToast.show();
    }

    public static GradientDrawable createShape(int color) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadius(Utils.dip2px(5));
        drawable.setColor(color);
        return drawable;


    }

    public static StateListDrawable createSelectorDrawable(Drawable pressedDrawable, Drawable normalDrawable) {
           /*	<selector xmlns:android="http://schemas.android.com/apk/res/android"  android:enterFadeDuration="200">
                <item  android:state_pressed="true" android:drawable="@drawable/detail_btn_pressed"></item>
                <item  android:drawable="@drawable/detail_btn_normal"></item>
            </selector>*/
        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressedDrawable);
        stateListDrawable.addState(new int[]{}, normalDrawable);// ̧����ʾ��ͼƬ
        return stateListDrawable;

    }
}
